class Register:
    """寄存器数据模型"""

    def __init__(self, address, name, unit="", data_type="uint16", value=0):
        self.address = address
        self.name = name
        self.unit = unit
        self.data_type = data_type
        self.value = value

    def formatted_value(self):
        """格式化寄存器值"""
        if self.data_type == "int16":
            return self.value if self.value < 32768 else self.value - 65536
        elif self.data_type == "uint16":
            return self.value
        elif self.data_type == "int32":
            return self.value  # 实际应用中需要组合两个寄存器
        elif self.data_type == "uint32":
            return self.value  # 实际应用中需要组合两个寄存器
        elif self.data_type == "float":
            # 实际应用中需要组合两个寄存器并转换
            return float(self.value)
        else:
            return self.value

    def to_dict(self):
        """转换为字典"""
        return {
            "address": f"0x{self.address:04X}",
            "name": self.name,
            "unit": self.unit,
            "type": self.data_type,
            "value": self.value
        }